<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
      <el-form :inline="true" :model="filters" :size="size">
        <el-form-item>
          <el-input v-model="filters.label" placeholder="用户名" />
        </el-form-item>
        <el-form-item>
          <kt-button
            icon="fa fa-search"
            :label="$t('action.search')"
            perms="sys:role:view"
            type="primary"
            @click="findPage(null)"
          />
        </el-form-item>
        <el-form-item>
          <kt-button
            icon="fa fa-plus"
            :label="$t('action.add')"
            perms="sys:user:add"
            type="primary"
            @click="handleAdd"
          />
        </el-form-item>
      </el-form>
    </div>

    <!--表格内容栏-->
    <kt-table
      :height="350"
      perms-edit="admin"
      perms-delete="admin"
      :data="pageResult"
      :columns="columns"
      @findPage="findPage"
      @handleEdit="handleEdit"
      @handleAdd="handleAdd"
      @handleDelete="deleteUser"
    />
    <!--新增编辑界面-->
    <el-dialog :title="operation?'新增':'编辑'" width="40%" :visible.sync="dialogVisible" :close-on-click-modal="false">
      <el-form
        ref="dataForm"
        :model="dataForm"
        label-width="80px"
        :rules="dataFormRules"
        :size="size"
        label-position="right"
      >
        <el-form-item v-if="false" label="ID" prop="id">
          <el-input v-model="dataForm.id" :disabled="!operation" auto-complete="off" />
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="dataForm.username" :disabled="!operation" auto-complete="off" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="dataForm.password" :disabled="!operation" type="password" auto-complete="off" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="dataForm.nickname" auto-complete="off" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="dataForm.email" auto-complete="off" />
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="dataForm.mobile" auto-complete="off" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :size="size" @click.native="dialogVisible = false">{{ $t('action.cancel') }}</el-button>
        <el-button :size="size" type="primary" :loading="editLoading" @click.native="submit">
          {{ $t('action.submit') }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import KtTable from '@/views/Core/KtTable'
import KtButton from '@/views/Core/KtButton'
import { format } from '@/utils/datetime'

export default {
  components: {
    KtTable,
    KtButton
  },
  data() {
    const reg = /^[A-Za-z0-9]{6,15}$/
    var validateInput = (rule, value, callback) => {
      if (!reg.test(value)) {
        callback(new Error('用户名只能包括数字字母的组合，长度为6-15位'))
      } else {
        callback()
      }
    }
    return {
      size: 'small',
      filters: {
        label: ''

      },

      columns: [
        { prop: 'id', label: 'ID', minWidth: 40, hidden: true },
        { prop: 'username', label: '用户名', minWidth: 50 },
        { prop: 'nickname', label: '姓名', minWidth: 50 },
        { prop: 'mobile', label: '手机', minWidth: 100 },
        { prop: 'email', label: '邮箱', minWidth: 150 },
        { prop: 'department', label: '部门', minWidth: 100 },
        { prop: 'gmtCreate', label: '', minWidth: 100 }
      ],
      pageRequest: { pageNumber: 1, pageSize: 10 },
      pageResult: {},

      operation: false, // true:新增, false:编辑
      dialogVisible: false, // 新增编辑界面是否显示
      editLoading: false,
      dataFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { validator: validateInput, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        nickname: [
          { required: true, message: '请输入昵称', trigger: 'blur' }
        ],
        email: [
          { type: 'email', message: '请输入正确邮箱', trigger: 'blur' }
        ]

      },
      // 新增编辑界面数据
      dataForm: {
        id: 0,
        name: '',
        password: '123456',
        nickname: '小明',
        province: '',
        city: '',
        area: '',
        address: '西北农林科技大学信息工程学院',
        email: 'swan1861@163.com',
        mobile: '12345678',
        status: 1,
        gmtCreate: null
      }
    }
  },
  mounted() {
    this.initColumns()
  },
  methods: {
    // 获取分页数据
    findPage: function(data) {
      if (data !== null) {
        this.pageRequest = data.pageRequest
      }
      this.pageRequest.username = this.filters.label
      this.$api.user.findPage(this.pageRequest).then((res) => {
        this.pageResult = res
        // this.findUserRoles()
      }).then(data != null ? data.callback : '')
    },
    // 加载用户角色信息
    findUserRoles: function() {
      this.$api.role.findAll().then((res) => {
        // 加载角色集合
        this.roles = res.data
      })
    },
    // 批量删除
    handleDelete: function(data) {
      this.$api.user.batchDelete(data.params).then(data != null ? data.callback : '')
    },
    //删除用户
    deleteUser: function (params) {
      //弹框询问
      const id = params.row.id
      this.$confirm('此操作将永久删除用户，是否继续？','提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.user.delById(id).then((res) => {
          this.findPage(null)
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      console.log(id)
    },
    // 显示新增界面
    handleAdd: function() {
      this.dialogVisible = true
      this.operation = true
      this.dataForm = {
        username: '',
        password: '',
        nickname: '',
        address: '',
        email: '',
        mobile: '',
        role: 2
      }
    },
    // 显示编辑界面
    handleEdit: function(params) {
      console.log(params)
      this.dialogVisible = true
      this.operation = false
      this.dataForm = Object.assign({}, params.row)
    },
    submit: function() {
      if (this.operation) {
        this.addSubmit()
      } else {
        this.updateSubmit()
      }
    },
    // 新增
    addSubmit: function() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认添加吗？', '提示', {}).then(() => {
            this.editLoading = true
            const params = Object.assign({}, this.dataForm)

            this.$api.user.add(params).then((res) => {
              this.editLoading = false
              if (res.code === 200) {
                this.$message({ message: '操作成功', type: 'success' })
                this.dialogVisible = false
                this.$refs['dataForm'].resetFields()
              } else {
                this.$message({ message: '操作失败, ' + res.msg, type: 'error' })
                this.editLoading = false
              }
              this.findPage(null)
            })
          })
        }
      })
    },
    // 编辑
    updateSubmit: function() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认修改吗？', '提示', {}).then(() => {
            this.editLoading = true
            const params = Object.assign({}, this.dataForm)

            this.$api.user.update(params).then((res) => {
              this.editLoading = false
              if (res.code === 200) {
                this.$message({ message: '操作成功', type: 'success' })
                this.dialogVisible = false
                this.$refs['dataForm'].resetFields()
              } else {
                this.$message({ message: '操作失败, ' + res.msg, type: 'error' })
                this.editLoading = false
              }
              this.findPage(null)
            })
          })
        }
      })
    },
    dateFormat: function(row, column, cellValue, index) {
      return format(row[column.property])
    },
    // 处理表格列过滤显示
    initColumns: function() {
      this.columns = [
        // {prop: "id", label: "ID", minWidth: 40,hidden:true},
        { prop: 'username', label: '用户名', minWidth: 50 },
        { prop: 'nickname', label: '姓名', minWidth: 60 },
        { prop: 'mobile', label: '手机', minWidth: 100 },
        { prop: 'email', label: '邮箱', minWidth: 100 },
        { prop: 'department', label: '部门', minWidth: 60 },
        { prop: 'gmtCreate', label: '创建时间', minWidth: 120, formatter: this.dateFormat }
      ]
    }

  }
}
</script>

<style scoped>

</style>
